package com.blog.myblog.interceptor;/*
    author dadaguai
    date:2022/3/14 14:35
*/

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    //重写preHandle方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果session中的uid为空，就说明还没有登录，则重定向到登录页面，阻止进入其他页面。
        //false:阻止；true:放行。
        if(request.getSession().getAttribute("user")==null) {
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }
}
